#!/usr/bin/make -f

# Turn on all hardening flags, as we're a networked daemon.
# Note: blhc (build log hardening check) will find these false positives: CPPFLAGS 2 missing, LDFLAGS 1 missing
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
# see EXAMPLES in dpkg-buildflags(1) and read /usr/share/dpkg/*
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk


%:
	dh $@

override_dh_auto_clean:
	dh_auto_clean
	rm -f dnslabeltext.cc
	chmod +x mkpubsuffixcc || true

override_dh_auto_configure:
	PATH=debian/configure-helpers/:$$PATH dh_auto_configure -- \
		--sysconfdir=/etc/powerdns \
		--enable-systemd --with-systemd=/lib/systemd/system \
		--enable-unit-tests \
		--disable-silent-rules \
		--with-service-user=pdns \
		--with-service-group=pdns \
		--with-libcap \
		--with-libsodium \
		--with-lua \
		--with-net-snmp \
		--enable-dns-over-tls \
		--enable-dnstap \
		--enable-nod

override_dh_auto_install:
	dh_auto_install
	install -d debian/pdns-recursor/usr/share/pdns-recursor/lua-config
	install -m 644 -t debian/pdns-recursor/usr/share/pdns-recursor/lua-config debian/lua-config/rootkeys.lua
	install -m 644 -t debian/pdns-recursor/etc/powerdns debian/recursor.lua
	install -d debian/pdns-recursor/usr/share/pdns-recursor/snmp
	install -m 644 -t debian/pdns-recursor/usr/share/pdns-recursor/snmp RECURSOR-MIB.txt
	rm -f debian/pdns-recursor/etc/powerdns/recursor.conf-dist
	rm -f debian/pdns-recursor/etc/powerdns/recursor.yml-dist
	./pdns_recursor --no-config --config=default | sed \
		-e 's!^# config-dir=.*!config-dir=/etc/powerdns!' \
		-e 's!^# hint-file=.*!&\nhint-file=/usr/share/dns/root.hints!' \
		-e 's!^# include-dir=.*!&\ninclude-dir=/etc/powerdns/recursor.d!' \
		-e 's!^# local-address=.*!local-address=127.0.0.1!' \
		-e 's!^# lua-config-file=.*!lua-config-file=/etc/powerdns/recursor.lua!' \
		-e 's!^# quiet=.*!quiet=yes!' \
		-e '/^# version-string=.*/d' \
		> debian/pdns-recursor/etc/powerdns/recursor.conf

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_test
	-cat testrunner.log
endif

override_dh_gencontrol:
	dh_gencontrol -- $(SUBSTVARS)

# Explicitly set a compression method, as Debian and Ubuntu defaults vary widely,
# and xz support is not available in all tools yet. Removing this override can
# make reprepro fail.
override_dh_builddeb:
	dh_builddeb -- -Zgzip
